#!/usr/bin/env python
# -*- coding: utf-8 -*-


# 该文件主要用于各种加密处理的方法

import base64

from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5
from Crypto.Cipher import PKCS1_v1_5 as Cipher_pkcs1_v1_5

from commons.config_tools import Cf



class EncryptTl:

    def __init__(self, secret_key=None):
        self.secret_key = secret_key  # 从后台获取的秘钥

    def rsa_passwd(self,user_pwd):
        """
        对明文密码进行rsa加密
        """
        public_key = base64.b64decode(self.secret_key).decode('utf-8')  # 将秘钥转为字符串明文
        pubkey = RSA.importKey(public_key)
        rsa_str = PKCS1_v1_5.new(pubkey)
        ras_pwd = base64.b64encode(rsa_str.encrypt(user_pwd.encode('utf-8'))).decode('utf-8')
        return ras_pwd


if __name__ == '__main__':
    a = 'LS0tLS1CRUdJTiBSU0EgUFVCTElDIEtFWS0tLS0tCk1JSUJJakFOQmdrcWhraUc5dzBCQVFFRkFBT0NBUThBTUlJQkNnS0NBUUVBM3BGSDhmZUpBdlI0TzBSZmNUeXIKVDlKcmtCOVhXUUxmZU9Rclo0bkpQeG1LQzUwaW9mSE9OT29YZGhhOWcrVXhjYklZay9pQlNpck9ZN0dNZjRQTgpWalZGWituSDdtWTRjMVVWdDVuRy9JKzdkSzZPLzd5QlpxUWdRLzFvOTFkdTJ1MTFneEpGTnVpZTVVcnc3RzBuCjZLYy95OUdXb1NNS1dkSHpDc1ZuWHNwUFYyQy9nM0tsR1paTmpvbnVZMWVUbUJwd3h1V3dTQitVd2E5cW1OU3kKcm5YVkpRUUVyREMxMXI4Z252U3hXOWx0Vy95RFdSdHNaUUhmZVkvZ3lTakppYWVEU3NYcUFMa1AzV2VWbGJHeQppZ2N3R2t4QzE2YlFHNmlCQVFZNFB2dWJoaXJCNUxwMXcrdllxUytMM0xSdzBIdTNIdWRXblg0d29NeGJ1dEpwClJ3SURBUUFCCi0tLS0tRU5EIFJTQSBQVUJMSUMgS0VZLS0tLS0K'
    b = "LS0tLS1CRUdJTiBSU0EgUFVCTElDIEtFWS0tLS0tCk1JSUJJakFOQmdrcWhraUc5dzBCQVFFRkFBT0NBUThBTUlJQkNnS0NBUUVBOU9xaWsyK1k3Z1VEWGpZemUxNGIKeEpDcHVxZGEyYndOODN0cVZJRS83blpFNlh1MnkwM21sSEg1YkVKaWEzSWoxODBlQ3ltMUVFT3hrUVFleFNEMgo5aENYSkp0dm9Tem1nWi9KbGdaYmNoKzlCNzBDSzM4K1FYQkZGaE53NUtLdkhwM0Q5M2FqbGxUQ3gvaW5keWFJCmwzNzhyWHI1MndYbDh0dVBMLzF0YWdNQk0yMXRvN29RY25UbTJpVGx6YU0rSjZheWRncjcrckFybit1ZWNSTnQKakRlTTdsQ2tFSmU4VDFUZjM3RzdCNHFQMXd4YjIvdVlkNkpXOEptZGlPMitkVk9vOFJHUWlPQVdtWWpNaG1UegpYVThnRDEwWksyTVVXMWd6QzdGcHNMNWc3UEd3NWMzSUtNLzYyV3prV3dCUlFWeENRVGNuVnN0SUd6WmNhUlpTCmN3SURBUUFCCi0tLS0tRU5EIFJTQSBQVUJMSUMgS0VZLS0tLS0K"
    passwd = 'Aa123456@'
    user_pwd = Cf().read_config_ini('user', 'user_pwd')
    print(EncryptTl(a).rsa_passwd(user_pwd))
    token = EncryptTl(b).rsa_passwd(passwd)
    print(token)
